﻿using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class CMRepositoy : ICMRepositoy
    {
        private readonly MyDbContext myDbContext;

        public CMRepositoy(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<int> CMAddAsync(CM cm)
        {
            await myDbContext.cMs.AddAsync(cm);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> CMDeleteAsync(int id)
        {
            CM cm = myDbContext.cMs.Find(id);
            myDbContext.cMs.Remove(cm);
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<int> CMDeleteAsync1(string id)
        {
            var entity = await myDbContext.cMs.FirstOrDefaultAsync(e => e.MajorKindId == id);

            if (entity != null)
            {
                // 移除找到的实体
                myDbContext.cMs.Remove(entity);

                // 保存更改到数据库
                return await myDbContext.SaveChangesAsync();
            }

            // 如果没有找到实体，则返回0表示没有进行任何删除操作
            return 0;
        }

        /// <summary>
        /// 查询全部职位数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<CM>> CMFillAsync()
        {
            return await myDbContext.cMs.ToListAsync();
        }

        public async Task<List<CM>> CMSelect(int pageIndex, int pageSize)
        {
            return await myDbContext.cMs.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public int CMSelectCount()
        {
            return myDbContext.cMs.Count();
        }

        public async Task<List<CM>> CMSelectId(string id)
        {
            return await myDbContext.cMs.Where(e => e.MajorKindId == id).ToListAsync();
        }

        public async Task<CM> CMSelectName(string name)
        {
            return await myDbContext.cMs.FirstOrDefaultAsync(e => e.MajorName == name);
        }

        public async Task<List<CMK>> CMSelectOpiton()
        {
            return await myDbContext.CMKs.ToListAsync();

        }

        /// <summary>
        /// 级联查询
        /// </summary>
        /// <returns></returns>
        public List<Cascader> Cascaders()
        {
            List<CMK> list = myDbContext.CMKs.ToList();//获取1级级结构表数据
            List<Cascader> list2 = new List<Cascader>();
            foreach (var c in list)
            {
                //获取2级数据
                List<CM> list3 = myDbContext.cMs.Where(e => e.MajorKindId == c.MajorKindId).ToList();
                List<Cascader> list4 = new List<Cascader>();
                foreach (var item in list3)
                {
                    Cascader cascader1 = new Cascader()
                    {
                        label = item.MajorName,
                        value = item.MajorId
                    };
                    list4.Add(cascader1);
                }
                Cascader cascader = new Cascader()
                {
                    value = c.MajorKindId,
                    label = c.MajorKindName,
                    children = list4
                };
                list2.Add(cascader);
            }
            return list2;
        }
    }
}
